//
//  UIView+Layer.m
//  APP
//
//  Created by huanchi on 15/12/22.
//  Copyright © 2015年 上海欢炽网络科技有限公司. All rights reserved.
//

#import "UIView+Layer.h"
#import <objc/runtime.h>

@implementation UIView (corner)

-(void)layerWithRadius:(CGFloat)radius{
    self.layer.cornerRadius = radius;
    self.layer.masksToBounds = YES;
}

-(void)layerWithRadius:(CGFloat)radius borderColor:(UIColor *)color borderWidth:(CGFloat)width{
    self.layer.borderColor = color.CGColor;
    self.layer.borderWidth = width;
    [self layerWithRadius:radius];
}

-(void)addLineLayerFromPoint:(CGPoint)start toPoint:(CGPoint)end withLineColor:(UIColor *)lineColor andLineHeight:(CGFloat)height{
    
    CALayer *layer = [CALayer layer];
    layer.bounds = CGRectMake(start.x, start.y, end.x-start.x, height);
    layer.position = CGPointMake((end.x-start.x)/2+start.x, height/2+start.y);
    layer.backgroundColor = lineColor.CGColor;
    [self.layer addSublayer:layer];
}

-(void)cutLayerCornerRadius:(CGFloat)cornerRadius{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    //设置大小
    CGRect rect = self.bounds;// CGRectMake(X(self), Y(self), ScreenWidth-30, HEIGH(self));
    maskLayer.frame = rect;
    //设置图形样子
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}
@end


static void *completionPropertyKey = (void *)@"completionPropertyKey";

@implementation UIView (animation)

-(void)animationWithType:(NSString *)type subType:(NSString *)subType duration:(float)duration{
    CATransition *animation = [CATransition animation];
    animation.duration = duration;
    animation.type = type;
    animation.subtype = subType;
    [self.layer addAnimation:animation forKey:nil];
}

-(completion)completionHandle{
    return objc_getAssociatedObject(self, completionPropertyKey);
}

-(void)setCompletionHandle:(completion)completionHandle{
    objc_setAssociatedObject(self, completionPropertyKey, completionHandle, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(void)curlAnimationTransition:(UIViewAnimationTransition)transition  delay:(double)delay  duration:(double)duration completion:(completion)completionHandle{
    [UIView beginAnimations:@"curl" context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    
    [UIView setAnimationTransition:transition forView:self cache:YES];
    [self animationDelay:delay duration:duration completion:completionHandle];
    [UIView commitAnimations];
    
}

-(void)scaleAnimationWithValue:(double)scaleValue delay:(double)delay duration:(double)duration completion:(completion)completionHandle{
    CGAffineTransform transform = CGAffineTransformScale(self.transform, scaleValue, scaleValue);
    [UIView beginAnimations:@"scale" context:NULL];
    [self animationDelay:delay duration:duration completion:completionHandle];
    self.alpha = 0;
    [self setTransform:transform];
    [UIView commitAnimations];
}


-(void)animationDelay:(double)delay duration:(double)duration completion:(completion)completionHandle{
    [UIView setAnimationDelay:delay];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationDelegate:self];
    self.completionHandle = completionHandle;
    [UIView setAnimationDidStopSelector:@selector(animationDidFinished)];
}

-(void)animationDidFinished{
    if (self.completionHandle) {
        self.completionHandle();
    }
}

@end
